#![allow(dead_code)]
use std::collections::HashMap;

use serde::{Deserialize, Deserializer, Serialize};

use super::illust::Illust;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Tag {
    pub name: String,
    pub translated_name: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TagDetail {
    pub name: String,
    pub is_registered: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TagIllust {
    #[serde(rename(deserialize = "tag"))]
    pub name: String,
    pub translated_name: Option<String>,
    pub illust: Illust,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Tags {
    pub tags: Vec<Tag>,
}

impl Tag {
    /// Проверяет есть ли в теге данная строка
    pub fn contains(&self, name: &str) -> bool {
        if self.name == name {
            true
        } else if let Some(translated) = &self.translated_name {
            translated == name
        } else {
            false
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TrendTags {
    #[serde(rename(deserialize = "trend_tags"))]
    pub tags: Vec<TagIllust>,
}

pub fn deserialize<'de, D>(d: D) -> Result<Vec<Tag>, D::Error>
where
    D: Deserializer<'de>,
{
    let a = <Vec<HashMap<String, Option<String>>>>::deserialize(d);
    match a {
        Ok(map_vec) => {
            let mut vec = Vec::new();
            for map in map_vec {
                let tag = Tag {
                    name: map["name"].clone().unwrap(),
                    translated_name: map["translated_name"].clone(),
                };
                vec.push(tag);
            }
            Ok(vec)
        }
        Err(err) => Err(err),
    }
}
